package library.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import library.dao.Impl.UserDaoImpl;
import library.pojo.User;

public class RegisterDialog extends JDialog {
	private JTextField userNameTextField;
	private JTextField passwordTextField;
	private JTextField confirmPasswordTextField;
	private JTextField nameTextField;
	private JTextField phoneTextField;
	public static void main(String[] args) {
		new RegisterDialog();
	}
	public RegisterDialog() {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.setTitle("注册");
		setBounds(100, 100, 440, 550);
		this.setLayout(null);
		
//		JPanel panel=(JPanel)this.getContentPane();
//		panel.setOpaque(false);
//		ImageIcon icon1 = new ImageIcon("res/2.png");
//		JLabel label = new JLabel(icon1);
//		label.setBounds(0, 0, this.getWidth(), this.getHeight());
//		icon1.setImage(icon1.getImage().getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_DEFAULT));
//		this.getLayeredPane().add(label,Integer.valueOf(Integer.MIN_VALUE));
		
		JLabel registerLabel = new JLabel("注册");
		registerLabel.setFont(new Font("宋体", Font.PLAIN, 40));
		registerLabel.setBounds(167, 26, 96, 59);
		this.add(registerLabel);
		
		JLabel userNameLabel = new JLabel("  用户名：");
		userNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		userNameLabel.setForeground(Color.black);
		userNameLabel.setBounds(25, 116, 112, 24);
		this.add(userNameLabel);
		
		JLabel passwordLabel = new JLabel("    密码：");
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		passwordLabel.setBounds(25, 176, 112, 24);
		this.add(passwordLabel);
		
		JLabel confirmPasswordLabel = new JLabel("确认密码：");
		confirmPasswordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		confirmPasswordLabel.setBounds(25, 236, 112, 24);
		this.add(confirmPasswordLabel);
		
		JLabel nameLabel = new JLabel("    姓名：");
		nameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		nameLabel.setBounds(25, 296, 112, 24);
		this.add(nameLabel);
		
		JLabel phoneLabel = new JLabel("联系电话：");
		phoneLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		phoneLabel.setBounds(25, 356, 112, 24);
		this.add(phoneLabel);
		
		userNameTextField = new JTextField();
		userNameTextField.setBounds(151, 119, 215, 22);
		this.add(userNameTextField);
		userNameTextField.setColumns(10);
		
		passwordTextField = new JTextField();
		passwordTextField.setColumns(10);
		passwordTextField.setBounds(151, 179, 215, 22);
		this.add(passwordTextField);
		
		confirmPasswordTextField = new JTextField();
		confirmPasswordTextField.setColumns(10);
		confirmPasswordTextField.setBounds(151, 239, 215, 22);
		this.add(confirmPasswordTextField);
		
		nameTextField = new JTextField();
		nameTextField.setColumns(10);
		nameTextField.setBounds(151, 299, 215, 22);
		this.add(nameTextField);
		
		phoneTextField = new JTextField();
		phoneTextField.setColumns(10);
		phoneTextField.setBounds(151, 359, 215, 22);
		this.add(phoneTextField);
		
		
		JButton registerButton = new JButton("注册");
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registerActionPerformed(e);
			}
		});
		registerButton.setBounds(138, 428, 167, 39);
		this.add(registerButton);
		
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}
	private void registerActionPerformed(ActionEvent e) {
		UserDaoImpl userDao = new UserDaoImpl();
		String userName = this.userNameTextField.getText();
		String password = new String (this.passwordTextField.getText());
		String confirmPassword = this.confirmPasswordTextField.getText();
		String phone = this.phoneTextField.getText();
		String name = this.nameTextField.getText();
		if(this.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
		return;
		}
		if(userDao.register(userName)!=null) {
			JOptionPane.showMessageDialog(null, "该用户已存在！");
			return;
		}
		if(this.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		if (password.length() <3 ) {
			JOptionPane.showMessageDialog(null, "密码长度不得小于3位！");
			return;
		}
		if(this.isEmpty(confirmPassword)) {
			JOptionPane.showMessageDialog(null, "请确保两次输入的密码一致！");
			return;
		}
		if(this.isNotSame(password, confirmPassword)) {
			JOptionPane.showMessageDialog(null, "请确保两次输入的密码一致！");
			return;
		}
		if(this.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "姓名不能为空！");
			return;
		}
		if(this.isEmpty(phone)) {
			JOptionPane.showMessageDialog(null, "联系电话不能为空！");
			return;
		}
		if (!phoneTextField.getText().matches("[0-9]{0,11}") || phoneTextField.getText().length() != 11) {
			JOptionPane.showMessageDialog(null, "请输入正确的手机号！");
			return;
		}
		
		User user = new User(userName,password,name,"借阅者",phone);
		userDao.addUser(user);
		JOptionPane.showMessageDialog(null, "注册成功！");
		this.dispose();
	}
	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if(str==null || "".equals(str.trim())) {
			return true;
		}
		else {
			return false;
		}
	}
	/**
	 * 判断两次输入的密码是否一致
	 */
	private boolean isNotSame(String str1,String str2) {
		if(str1.equals(str2)) {
			return false;
		}else {
			return true;
		}
	}
	}


